package com.anti_poverty_detection_system.springboot.controller;

import com.anti_poverty_detection_system.springboot.mapper.RegionUpperMapper;
import com.anti_poverty_detection_system.springboot.service.dto.CascaderOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZITONG BAI
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/region_upper")
public class RegionUpperController {
    @Autowired
    RegionUpperMapper regionUpperMapper;

    @GetMapping("region_upper_options")
    public List<CascaderOption> getRegionUpperOptions() {
        List<CascaderOption> options = new ArrayList<>();
        List<String> provinces = regionUpperMapper.findProvinces();

        // 常规省份和城市处理
        for (String province : provinces) {
            CascaderOption provinceOption = new CascaderOption(province, province);
            List<String> cities = regionUpperMapper.findCitiesByProvince(province);
            List<CascaderOption> cityOptions = new ArrayList<>();
            for (String city : cities) {
                CascaderOption cityOption = new CascaderOption(city, city);
                List<String> counties = regionUpperMapper.findCountiesByCity(city);
                List<CascaderOption> countyOptions = new ArrayList<>();
                for (String county : counties) {
                    countyOptions.add(new CascaderOption(county, county));
                }
                cityOption.setChildren(countyOptions);
                cityOptions.add(cityOption);
            }
            provinceOption.setChildren(cityOptions);
            options.add(provinceOption);
        }

        // 处理直辖市
        String[] municipalities = {"北京市", "上海市", "天津市", "重庆市"};
        for (String municipality : municipalities) {
            CascaderOption municipalityOption = new CascaderOption(municipality, municipality);
            List<String> counties = regionUpperMapper.findCountiesByCity(municipality);
            List<CascaderOption> countyOptions = new ArrayList<>();
            for (String county : counties) {
                countyOptions.add(new CascaderOption(county, county));
            }
            municipalityOption.setChildren(countyOptions);
            options.add(municipalityOption);
        }

        return options;
    }

    // 全部级联
    @GetMapping("region_upper_options2")
    public List<CascaderOption> getRegionUpperOptions2() {
        List<CascaderOption> options = new ArrayList<>();
        List<String> provinces = regionUpperMapper.findProvinces();

        // 常规省份和城市处理
        for (String province : provinces) {
            CascaderOption provinceOption = new CascaderOption(province, province);
            List<String> cities = regionUpperMapper.findCitiesByProvince(province);
            List<CascaderOption> cityOptions = new ArrayList<>();
            for (String city : cities) {
                CascaderOption cityOption = new CascaderOption(city, city);
                List<String> counties = regionUpperMapper.findCountiesByCity(city);
                List<CascaderOption> countyOptions = new ArrayList<>();
                for (String county : counties) {
                    CascaderOption countyOption = new CascaderOption(county, county);
                    List<String> townships = regionUpperMapper.findTownshipsByCounty(county);
                    List<CascaderOption> townshipOptions = new ArrayList<>();
                    for (String township : townships) {
                        CascaderOption townshipOption = new CascaderOption(township, township);
                        List<String> villages = regionUpperMapper.findVillagesByTownShip(township);
                        List<CascaderOption> villageOptions = new ArrayList<>();
                        for (String village : villages) {
                            villageOptions.add(new CascaderOption(village, village));
                        }
                        townshipOption.setChildren(villageOptions);
                        townshipOptions.add(townshipOption);
                    }
                    countyOption.setChildren(townshipOptions);
                    countyOptions.add(countyOption);
                }
                cityOption.setChildren(countyOptions);
                cityOptions.add(cityOption);
            }
            provinceOption.setChildren(cityOptions);
            options.add(provinceOption);
        }

        // 处理直辖市
        String[] municipalities = {"北京市", "上海市", "天津市", "重庆市"};
        for (String municipality : municipalities) {
            CascaderOption municipalityOption = new CascaderOption(municipality, municipality);
            List<String> counties = regionUpperMapper.findCountiesByCity(municipality);
            List<CascaderOption> countyOptions = new ArrayList<>();
            for (String county : counties) {
                countyOptions.add(new CascaderOption(county, county));
            }
            municipalityOption.setChildren(countyOptions);
            options.add(municipalityOption);
        }

        return options;
    }

}
